package cn.sccl.mybatis.junt;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import cn.sccl.mybatis.dao.UserDao;
import cn.sccl.mybatis.dao.UserDaoImpl;
import cn.sccl.mybatis.pojo.User;

/**
 * @author 饶爽
 * @time 2019年9月15日 下午10:40:09
 * @version 1.0.0
 * 原生Dao测试
*/

public class MybatisDaoTest {
    
    public SqlSessionFactory sqlSessionFactory;
    @Before
    public void before() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
    }
    @Test
    //测试根据id查询用户
    public void testDao() throws Exception{
        UserDao dao = new UserDaoImpl(sqlSessionFactory);
        User user = dao.selectUserById(10);
        System.out.println(user);
    }
    @Test
    //测试根据用户名模糊查询用户
    public void testDao2() throws Exception{
        UserDao dao = new UserDaoImpl(sqlSessionFactory);
        List<User> list = dao.queryUserByUsername("王");
        for (User user2 : list) {
            System.out.println(user2);
        }
    }
    @Test
    //测试保存用户
    public void testDao3() throws Exception{
        UserDao dao = new UserDaoImpl(sqlSessionFactory);
        
        User user = new User();
        user.setUsername("李白");
        user.setAddress("西安");
        
        dao.saveUser(user);
    }
}
